<?php

namespace App\DB\Steam;

use App\DB\CommonDB;
use App\Model\Steam\SteamGame;
use App\Model\Steam\SteamGameCollocation;
use App\Model\Steam\SteamGameGoods;
use App\Model\Steam\SteamGamePicture;
use App\Model\Steam\SteamNews;
use Illuminate\Support\Facades\DB;

class SteamGameDB
{
    /**
     * 为“商品列表”填写“标签列表”项
     * @param $data
     */
    public static function getListTag($data){
        $gameIds = [];
        foreach ($data as $key => $item) {
            $gameIds[] = $item['id'];
            $data[$key]['tag'] = [];
        }
        $mapList = DB::table('steam_game_tag_map as map')->select('map.id', 'map.game_id', 'map.tag_id', 'tag.title')
            ->leftJoin('steam_game_tag as tag', 'map.tag_id', '=', 'tag.id')
            ->whereIn('map.game_id', $gameIds)
            ->orderBy('map.game_id')
            ->get();
        if($mapList) {
            $tagData = [];
            foreach ($mapList as $key => $val) {
//                $tagData[$val->game_id][$val->id] = $val->title;
                $tagData[$val->game_id][] = [
                    'title' => $val->title,
                    'id' => $val->id
                ];
            }
            foreach ($data as $key => $item) {
                if(isset($tagData[$item['id']])) {
                    $data[$key]['tag'] = $tagData[$item['id']];
                }
            }
        }
        return $data;
    }

    /**
     * 浏览游戏
     *
     * @param $id
     * @param $start
     * @param $limit
     * @return mixed
     */
    public static function browseGame($tab = 'new', $category = 0, $tag = 0, $page = 1, $limit = 10)
    {
        $result = SteamGame::select('id', 'title', 'issue_date', 'introduction', 'price', 'is_discount', 'discount_price', 'discount_start', 'discount_end', 'thumb', 'is_windows', 'is_macos', 'is_linux', 'is_free')->where('is_delete', 0);
        if($category != 0) {
            $mapData = DB::table('steam_game_category_map')->select('game_id')->where('category_id', $category)->get();
            if (empty($mapData)) {
                $respone = [
                    'data' => [],
                    'count' => 0,
                    'page_count' => 0
                ];
                return $respone;
            }
            $gameIds = [];
            foreach ($mapData as $key => $val) {
                $gameIds[] = $val->game_id;
            }
            $result = $result ->whereIn('id', $gameIds);
        }
        if($tag != 0) {
            $mapTagData = DB::table('steam_game_tag_map')->select('game_id')->where('tag_id', $tag)->get();
            if (empty($mapTagData)) {
                $respone = [
                    'data' => [],
                    'count' => 0,
                    'page_count' => 0
                ];
                return $respone;
            }
            $gameIds = [];
            foreach ($mapTagData as $key => $val) {
                $gameIds[] = $val->game_id;
            }
            $result = $result ->whereIn('id', $gameIds);
        }
        switch ($tab) {
            case 'new':
                $result = $result->where('is_new', 1)->orderBy('issue_date', 'desc');
                break;
            case 'hot':
                $result = $result->where('is_hot', 1)->orderBy('issue_date', 'desc');
                break;
            case 'soon':
                $result = $result->where('issue_date', '>', currentTime())->orderBy('issue_date', 'asc');
                break;
            case 'discount':
                $result = $result->where('is_discount', 1)->orderBy('discount_end', 'asc');
                break;
            default:
                $result = $result->orderBy('issue_date', 'desc');
                break;
        }
        $gameList = CommonDB::page($result, $page, $limit);
        $gameList['data'] = self::getListTag($gameList['data']);
        if(!$gameList) {
            return false;
        }
        return $gameList;
    }


    /**
     * 游戏详情
     *
     * @param $id
     * @return mixed
     */
      public static function getGameDetails($id)
      {
          # 查看游戏数据
          $steamGame = SteamGame::where('is_delete', 0)->find($id);
          if(!$steamGame) {
              # 没有则直接返回异常
              return false;
          }
          # 保存游戏基本数据
          $data = [
              'id' => $id,
              'title' => $steamGame->title,
              'issue_date' => $steamGame->issue_date,
              'introduction' => $steamGame->introduction,
              'thumb' => $steamGame->thumb,
              'price' => $steamGame->price,
              'is_discount' => $steamGame->is_discount,
              'discount_price' => $steamGame->discount_price,
              'discount_start' => $steamGame->discount_start,
              'discount_end' => $steamGame->discount_end,
              'description' => $steamGame->description,
              'picture' => [],
              'goods' => [],
              'tag' => [],
              'news' => [],
              'system' => []
          ];
          # 查询游戏图片数据
          $steamGamePicture = SteamGamePicture::select('id', 'img_name', 'img_original', 'img_url')->where('game_id', $id)->get();
          $data['picture'] = ($steamGamePicture) ? $steamGamePicture->toArray() : [];
          # 查询游戏商品数据
          $steamGameGoods =  SteamGameGoods::select('id', 'title', 'price', 'type')->where('game_id', $id)->get();
          $data['goods'] = ($steamGameGoods) ? $steamGameGoods->toArray() : [];
          # 查询游戏的全局标签
          $tagMap = DB::table('steam_game_tag_map as map')->select('map.tag_id', 'tag.title')
              ->leftJoin('steam_game_tag as tag', 'map.tag_id', '=', 'tag.id')->where('map.game_id', $id)->groupBy('map.tag_id')->get();
          $tags = [];
          foreach ($tagMap as $key => $val) {
//              $tags[$val->tag_id] = $val->title;
              $tags[] = [
                  'tag_id' => $val->tag_id,
                  'tag_title' => $val->title
              ];
          }
          $data['tag'] = $tags;
          # 查询游戏的相关新闻
          $steamNews = SteamNews::select('id', 'title', 'issue_date', 'author', 'content')->where('game_id', $id)->orderBy('issue_date', 'desc')->take(2)->get();
          $data['news'] = ($steamNews) ? $steamNews->toArray() : [];
          # 查询游戏的配置
          $steamGameCollocation = SteamGameCollocation::select('id', 'type', 'content')->where('game_id', $id)->get();
          $data['system'] = ($steamGameCollocation) ? $steamGameCollocation->toArray() : [];
          return $data;
      }
}
